using System;
using System.Linq;
using DevExpress.Xpo;
using DevExpress.ExpressApp;
using DevExpress.Data.Filtering;
using DevExpress.ExpressApp.Xpo;
using DevExpress.Persistent.Base;
using DevExpress.ExpressApp.Updating;
using DevExpress.Persistent.BaseImpl;
using DevExpress.ExpressApp.Security;
using DevExpress.ExpressApp.Workflow.Xpo;
//using DevExpress.ExpressApp.Reports;
//using DevExpress.ExpressApp.PivotChart;
//using DevExpress.ExpressApp.Security.Strategy;
//using WorkFlowDemo.Module.BusinessObjects;

namespace WorkFlowDemo.Module.DatabaseUpdate
{
    // For more typical usage scenarios, be sure to check out http://documentation.devexpress.com/#Xaf/clsDevExpressExpressAppUpdatingModuleUpdatertopic
    public class Updater : ModuleUpdater
    {
        public Updater(IObjectSpace objectSpace, Version currentDBVersion) :
            base(objectSpace, currentDBVersion)
        {
        }
        public override void UpdateDatabaseAfterUpdateSchema()
        {
            base.UpdateDatabaseAfterUpdateSchema();
            //string name = "MyName";
            //DomainObject1 theObject = ObjectSpace.FindObject<DomainObject1>(CriteriaOperator.Parse("Name=?", name));
            //if(theObject == null) {
            //    theObject = ObjectSpace.CreateObject<DomainObject1>();
            //    theObject.Name = name;
            //}
        }
        public override void UpdateDatabaseBeforeUpdateSchema()
        {
            base.UpdateDatabaseBeforeUpdateSchema();
            //if(CurrentDBVersion < new Version("1.1.0.0") && CurrentDBVersion > new Version("0.0.0.0")) {
            //    RenameColumn("DomainObject1Table", "OldColumnName", "NewColumnName");
            //}

            CreateDemoObjects();
        }

        private void CreateDemoObjects()
        {
            if (ObjectSpace.GetObjects<XpoWorkflowDefinition>().Count == 0)
            {
                XpoWorkflowDefinition receiveCorrelationsActivityDefinition = ObjectSpace.CreateObject<XpoWorkflowDefinition>();
                receiveCorrelationsActivityDefinition.Name = "Start/stop (correlations) demo";
                receiveCorrelationsActivityDefinition.Xaml = ReceiveCorrelationsXaml;
                receiveCorrelationsActivityDefinition.TargetObjectType = typeof(WorkFlowDemo.Module.BusinessObjects.Task);
                receiveCorrelationsActivityDefinition.IsActive = true;
            }
            ObjectSpace.CommitChanges();
        }


        private const string ReceiveCorrelationsXaml =
            @"<Activity mc:Ignorable=""sap"" x:Class=""DevExpress.Workflow.xWF1"" sap:VirtualizedContainerService.HintSize=""330,1198"" mva:VisualBasic.Settings=""Assembly references and imported namespaces for internal implementation"" xmlns=""http://schemas.microsoft.com/netfx/2009/xaml/activities"" xmlns:dpb=""clr-namespace:DevExpress.Persistent.BaseImpl;assembly=DevExpress.Persistent.BaseImpl.v13.2"" xmlns:dwa=""clr-namespace:DevExpress.Workflow.Activities;assembly=DevExpress.Workflow.Activities.v13.2"" xmlns:dx=""clr-namespace:DevExpress.Xpo;assembly=DevExpress.Xpo.v13.2"" xmlns:dx1=""clr-namespace:DevExpress.Xpo;assembly=DevExpress.Data.v13.2"" xmlns:dxh=""clr-namespace:DevExpress.Xpo.Helpers;assembly=DevExpress.Xpo.v13.2"" xmlns:dxh1=""clr-namespace:DevExpress.Xpo.Helpers;assembly=DevExpress.Data.v13.2"" xmlns:dxmh=""clr-namespace:DevExpress.Xpo.Metadata.Helpers;assembly=DevExpress.Xpo.v13.2"" xmlns:mc=""http://schemas.openxmlformats.org/markup-compatibility/2006"" xmlns:mv=""clr-namespace:Microsoft.VisualBasic;assembly=System"" xmlns:mva=""clr-namespace:Microsoft.VisualBasic.Activities;assembly=System.Activities"" xmlns:p=""http://schemas.microsoft.com/netfx/2009/xaml/servicemodel"" xmlns:s=""clr-namespace:System;assembly=mscorlib"" xmlns:s1=""clr-namespace:System;assembly=System"" xmlns:s2=""clr-namespace:System;assembly=System.Xml"" xmlns:s3=""clr-namespace:System;assembly=System.Core"" xmlns:s4=""clr-namespace:System;assembly=System.ServiceModel"" xmlns:sa=""clr-namespace:System.Activities;assembly=System.Activities"" xmlns:sad=""clr-namespace:System.Activities.Debugger;assembly=System.Activities"" xmlns:sap=""http://schemas.microsoft.com/netfx/2009/xaml/activities/presentation"" xmlns:scg=""clr-namespace:System.Collections.Generic;assembly=System"" xmlns:scg1=""clr-namespace:System.Collections.Generic;assembly=System.ServiceModel"" xmlns:scg2=""clr-namespace:System.Collections.Generic;assembly=System.Core"" xmlns:scg3=""clr-namespace:System.Collections.Generic;assembly=mscorlib"" xmlns:sd=""clr-namespace:System.Data;assembly=System.Data"" xmlns:sl=""clr-namespace:System.Linq;assembly=System.Core"" xmlns:ssa=""clr-namespace:System.ServiceModel.Activities;assembly=System.ServiceModel.Activities"" xmlns:ssx=""clr-namespace:System.ServiceModel.XamlIntegration;assembly=System.ServiceModel"" xmlns:st=""clr-namespace:System.Text;assembly=mscorlib"" xmlns:wmo=""clr-namespace:WorkFlowDemo.Module.BusinessObjects;assembly=WorkFlowDemo.Module"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
  <Sequence DisplayName=""Main"" sap:VirtualizedContainerService.HintSize=""290,1158"">
    <Sequence.Variables>
      <Variable x:TypeArguments=""p:CorrelationHandle"" Name=""clientIdHandle"" />
      <Variable x:TypeArguments=""x:String"" Name=""localClientId"" />
      <Variable x:TypeArguments=""s:Guid"" Name=""taskId"" />
    </Sequence.Variables>
    <sap:WorkflowViewStateService.ViewState>
      <scg3:Dictionary x:TypeArguments=""x:String, x:Object"">
        <x:Boolean x:Key=""IsExpanded"">True</x:Boolean>
      </scg3:Dictionary>
    </sap:WorkflowViewStateService.ViewState>
    <p:Receive x:Name=""__ReferenceID0"" CanCreateInstance=""True"" CorrelatesWith=""[clientIdHandle]"" sap:VirtualizedContainerService.HintSize=""268,86"" OperationName=""Start"" ServiceContractName=""IStartStop"">
      <p:Receive.CorrelatesOn>
        <p:XPathMessageQuery x:Key=""key1"">
          <p:XPathMessageQuery.Namespaces>
            <ssx:XPathMessageContextMarkup>
              <x:String x:Key=""xgSc"">http://tempuri.org/</x:String>
            </ssx:XPathMessageContextMarkup>
          </p:XPathMessageQuery.Namespaces>sm:body()/xgSc:Start/xgSc:clientId</p:XPathMessageQuery>
      </p:Receive.CorrelatesOn>
      <p:ReceiveParametersContent>
        <OutArgument x:TypeArguments=""x:String"" x:Key=""clientId"">[localClientId]</OutArgument>
      </p:ReceiveParametersContent>
    </p:Receive>
    <dwa:ObjectSpaceTransactionScope AutoCommit=""True"" sap:VirtualizedContainerService.HintSize=""268,333"">
      <dwa:ObjectSpaceTransactionScope.Variables>
        <Variable x:TypeArguments=""wmo:Task"" Name=""task"" />
      </dwa:ObjectSpaceTransactionScope.Variables>
      <dwa:CreateObject x:TypeArguments=""wmo:Task"" sap:VirtualizedContainerService.HintSize=""242,22"" Result=""[task]"" />
      <Assign sap:VirtualizedContainerService.HintSize=""242,58"">
        <Assign.To>
          <OutArgument x:TypeArguments=""x:String"">[task.Subject]</OutArgument>
        </Assign.To>
        <Assign.Value>
          <InArgument x:TypeArguments=""x:String"">[""Task for client "" + localClientId]</InArgument>
        </Assign.Value>
      </Assign>
      <dwa:CommitChanges sap:VirtualizedContainerService.HintSize=""242,22"" />
      <Assign sap:VirtualizedContainerService.HintSize=""242,58"">
        <Assign.To>
          <OutArgument x:TypeArguments=""s:Guid"">[taskId]</OutArgument>
        </Assign.To>
        <Assign.Value>
          <InArgument x:TypeArguments=""s:Guid"">[task.Oid]</InArgument>
        </Assign.Value>
      </Assign>
    </dwa:ObjectSpaceTransactionScope>
    <p:SendReply Request=""{x:Reference __ReferenceID0}"" DisplayName=""SendReplyToReceive"" sap:VirtualizedContainerService.HintSize=""268,86"">
      <p:SendParametersContent>
        <InArgument x:TypeArguments=""x:String"" x:Key=""result"">[""Task is created for client "" + localClientId + "".""]</InArgument>
      </p:SendParametersContent>
    </p:SendReply>
    <p:Receive x:Name=""__ReferenceID1"" CorrelatesWith=""[clientIdHandle]"" sap:VirtualizedContainerService.HintSize=""268,86"" OperationName=""Stop"" ServiceContractName=""IStartStop"">
      <p:Receive.CorrelatesOn>
        <p:XPathMessageQuery x:Key=""key1"">
          <p:XPathMessageQuery.Namespaces>
            <ssx:XPathMessageContextMarkup>
              <x:String x:Key=""xgSc"">http://tempuri.org/</x:String>
            </ssx:XPathMessageContextMarkup>
          </p:XPathMessageQuery.Namespaces>sm:body()/xgSc:Stop/xgSc:clientId</p:XPathMessageQuery>
      </p:Receive.CorrelatesOn>
      <p:ReceiveParametersContent>
        <OutArgument x:TypeArguments=""x:String"" x:Key=""clientId"" />
      </p:ReceiveParametersContent>
    </p:Receive>
    <dwa:ObjectSpaceTransactionScope AutoCommit=""True"" sap:VirtualizedContainerService.HintSize=""268,157"">
      <dwa:DeleteObject x:TypeArguments=""wmo:Task"" sap:VirtualizedContainerService.HintSize=""200,59"" Key=""[taskId]"" />
    </dwa:ObjectSpaceTransactionScope>
    <p:SendReply Request=""{x:Reference __ReferenceID1}"" DisplayName=""SendReplyToReceive"" sap:VirtualizedContainerService.HintSize=""268,86"">
      <p:SendParametersContent>
        <InArgument x:TypeArguments=""x:String"" x:Key=""result"">[""Task for client "" + localClientId + "" was deleted.""]</InArgument>
      </p:SendParametersContent>
    </p:SendReply>
  </Sequence>
</Activity>
";

    }
}
